<template>
  <page-header-wrapper>
    <page-tab-list>
      <div class="table-page-search-box">
        <a-form layout="inline">
          <a-row :gutter="24">
            <a-col :md="8" :sm="24">
              <a-form-item label="订单号码">
                <a-input v-model="queryParam.order_no" placeholder="请输入订单号码"></a-input>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="起止时间">
                <a-range-picker
                  :show-time="{ format: 'HH:mm' }"
                  format="YYYY-MM-DD HH:mm"
                  valueFormat="YYYY-MM-DD HH:mm"
                  :placeholder="['开始时间', '结束时间']"
                  v-model="queryTime"
                  @change="changeTime"
                  style="width: 100%"
                />
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="业务员">
                <div class="flex">
                  <a-select placeholder="请选择" v-model="queryParam.salesman_id" allowClear>
                    <a-select-option v-for="item of salesmanList" :key="item.id" :value="item.id">
                      {{ item.name }}
                    </a-select-option>
                  </a-select>
                  <a-button type="primary" @click="getList">查询</a-button>
                </div>
              </a-form-item>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </page-tab-list>
    <a-card :bordered="false" class="card_style_re">
      <div class="table-operator" style="display: flex; justify-content: space-between">
        <a-radio-group v-model="queryParam.type" @change="getList">
          <a-radio-button value="-1"> 全部 </a-radio-button>
          <a-radio-button value="1"> 收入 </a-radio-button>
          <a-radio-button value="2"> 支出 </a-radio-button>
        </a-radio-group>
      </div>
      <a-table
        :rowKey="
          (r, i) => {
            return r.id
          }
        "
        :pagination="{
          total: this.totalCount,
          showSizeChanger: false,
          onChange: this.tablePageChange,
        }"
        :columns="columns"
        :data-source="list"
        :scroll="{ x: 'max-content' }"
      >
        <div slot="status" slot-scope="text">
          <div class="status">
            <span :class="text == 2 ? 'warning' : 'open'"></span>
            <span>{{ text == 2 ? '支出' : '收入' }}</span>
          </div>
        </div>
      </a-table>
    </a-card>
  </page-header-wrapper>
</template>

<script>
import { incomeLog } from '@/api/saas/agency'
import { getList } from '@/api/saas/salesman'

const columns = [
  {
    title: '订单号码',
    dataIndex: 'order_no',
  },
  {
    title: '金额',
    dataIndex: 'commission',
    sorter: (a, b) => parseFloat(a.commission) - parseFloat(b.commission),
  },
  {
    title: '状态',
    dataIndex: 'type',
    scopedSlots: {
      customRender: 'status',
    },
  },
  {
    title: '说明',
    dataIndex: 'desc',
  },
  {
    title: '操作时间',
    dataIndex: 'created_at',
  },
]

export default {
  data() {
    return {
      // 搜索参数
      queryParam: {
        order_no: '',
        type: '-1',
        pageNo: 1,
        pageSize: 10,
        start_date: '',
        end_date: '',
        salesman_id: '',
      },
      selectedRowKeys: [],
      columns,
      list: [],
      btnLoading: false,
      // 查询时间
      queryTime: [],
      salesmanList: [],
    }
  },
  created() {
    getList({
      pageSize: 99999,
      pageNo: 1,
      status: '-1',
    })
      .then((res) => {
        if (!res.code) {
          this.salesmanList = res.data.data
        }
      })
      .catch((err) => {
        this.$message.error(err.message)
        this.$refs.table.localLoading = false
      })

    this.getList()
  },
  methods: {
    // 分页请求
    tablePageChange(e) {
      this.queryParam.pageNo = e
      this.getList()
    },
    // 获取数据
    async getList() {
      const hide = this.$message.loading('加载中', 0)
      const res = await incomeLog(this.queryParam)
      console.log(res)
      if (res.code == 0) {
        this.totalCount = res.data.totalCount
        this.list = res.data.data
      }
      hide()
    },
    // 切换筛选时间
    changeTime(e) {
      if (e.length) {
        this.queryParam.start_date = e[0]
        this.queryParam.end_date = e[1]
      } else {
        this.queryParam.start_date = null
        this.queryParam.end_date = null
      }
    },
  },
}
</script>
<style lang="less" scoped>
.status {
  display: flex;
  align-items: center;
  span:nth-child(1) {
    width: 6px;
    height: 6px;
    border-radius: 50%;
    margin-right: 4px;
  }
  .close {
    background-color: #ff4400;
  }
  .open {
    background-color: #65c817;
  }
  .warning {
    background-color: #eeb041;
  }
}
</style>
